package com.sysd.mail.util;

import org.xbill.DNS.Lookup;
import org.xbill.DNS.MXRecord;
import org.xbill.DNS.Record;
import org.xbill.DNS.Type;

import java.util.ArrayList;
import java.util.List;

/**
 * project: mail
 *
 * @author bsh
 * @description LookDNSMX
 * @dataTime 2023/3/30 14:36
 * @company 丽水信阳软件科技有限公司
 */
public class DNSUtil {
    public static MXRecord[] getMxRecords(String domain) {
        try {
            List<MXRecord> list = new ArrayList<MXRecord>();
            Lookup lookup = new Lookup(domain, Type.MX);
            lookup.run();
            if (lookup.getResult() == Lookup.SUCCESSFUL) {
                for (Record mx : lookup.getAnswers()) {
                    list.add((MXRecord) mx);
                }
            }
            return list.toArray(new MXRecord[0]);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}